﻿ var App_DEPARTModel = $.extend(TradeModel, {
	 editor:{
		    title: "弹出框名称",
			winid:'App_DEPART_EditForm',
			formid:'App_DEPART_EditForm_fm',
			width:720,
			height:300
		},   
	    url: '/sysmgr/depart', 
	    title: "部门",
	    idField: "departId",
	    titleField: "departName",
	    toolbar: 'App_DEPART_Toolbar',
	    datagrid: 'App_DEPART_Grid', 
    selRow: null 
}); 
 
 App_DEPARTModel.FindRowById=function(id) {  
	    return fun_FindRowById(  id);
}

 App_DEPARTModel.reload =function() {
	fun_ReloadApp_DEPARTList();
    $('#App_DEPART_EditForm').dialog('close');
    $('#App_DEPART_Grid').treegrid('reload');
} 


function fun_FindRowById(id) {
    return $('#App_DEPART_Grid').treegrid('find', id);
}


function fun_ShowApp_DEPARTEditForm(id) { 
    if (typeof (id) != undefined && typeof (id) == 'object') {
        App_DEPARTModel.postAction = 'update';
        App_DEPARTModel.selRow = id;
    }
    else if (id == -2) {
    	var pid=$.cookie('departCode');
    	if(!pid)pid='-1';
        App_DEPARTModel.postAction = 'add';
        App_DEPARTModel.selRow={};
        App_DEPARTModel.selRow = App_DEPARTModel.model;
        App_DEPARTModel.selRow.pDepartId = pid;
    }
    else if (id == -1) {
        App_DEPARTModel.postAction = 'add';
        App_DEPARTModel.selRow={};
        var parentNode = $('#App_DEPART_Grid').treegrid('getSelected');
        App_DEPARTModel.selRow = {};
        if (parentNode) {
            App_DEPARTModel.selRow.pDepartId = parentNode['departId'];
        }
    }
    else {
        App_DEPARTModel.postAction = 'update';
        App_DEPARTModel.selRow = fun_FindRowById(id);
    }  
    
    $('#App_DEPART_EditForm_fm').form('clear' );
    $('#App_DEPART_EditForm_fm').form('load', App_DEPARTModel.selRow);
    $('#App_DEPART_EditForm').dialog({
        top: "50px",
        resizable: true,
        maximizable: true,
        width:700,
        height: 410
    })
    .dialog('open')
    .dialog('setTitle', ((App_DEPARTModel.postAction == 'add') ? '新增'  : '修改' ) );
}
function fun_SaveApp_DEPART() {
	App_DEPARTModel.submit();
}
function fun_DelApp_DEPART(title, id,state) {
	if(state!='closed')
		App_DEPARTModel.del(id,title);
	else
		App_DEPARTModel.ShowMsg('提示','请先删除下级部门！');
	
}

function fun_App_DEPARTSearchbox(value, name) {
    if (value == "" || value == null) {
        $.messager.alert('提示', '请输入关键字!', 'info');
    }
    else {
        $('#App_DEPART_Grid').treegrid('reload');
    }

}
function fun_ReloadApp_DEPARTList() {
    $('#App_DEPART_Grid').treegrid('reload');
}

function fun_ReloadNodeList() {
    var row = $('#App_DEPART_Grid').tree('getSelected');
    $('#App_DEPART_Grid').tree('reload', row[App_DEPARTModel.idField]);
}



$(function () {
    $('#App_DEPART_Grid').treegrid({
        fit: true,
        singleSelect: true,
        fitColumns: true,
        method: 'post',
        idField: App_DEPARTModel.idField,
        treeField: App_DEPARTModel.titleField, 
        url: Onluy.basePath()+App_DEPARTModel.url+'/tree',
        loader:App_DEPARTModel.Loader,
	    loadFilter: App_DEPARTModel.loadfilter,
        toolbar: '#App_DEPART_Toolbar',
        columns: [
              [

                  { field: 'departName', title: '名称', width: 300, formatter: function (value, row) { return value + '&nbsp;&nbsp;&nbsp;<span style="color:#cecece">(&nbsp;' + row['departCode'] + '&nbsp;)</span>'; } },

                  { field: 'departPhone', title: '联系电话', width: 120 },
					{ field: 'startTime', title: '生效日期', width: 120, formatter: formatTime },
					{ field: 'endTime', title: '失效日期', width: 120, formatter: formatTime },
                    {
					    field: 'A', title: '操作', width: 80,
					    formatter: function (value, rec) {
					        return ' <img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/pencil.gif" onclick="fun_ShowApp_DEPARTEditForm(\'' + rec[App_DEPARTModel.idField] + '\');" title="修改' + rec[App_DEPARTModel.titleField] + '" />'
                            + '<img class="btn-16" src="'+Onluy.basePath()+'/Images/icos/16/delete.gif" onclick="fun_DelApp_DEPART(\'' + rec[App_DEPARTModel.titleField] + '\',\'' + rec[App_DEPARTModel.idField] + '\',\'' + rec['state'] + '\');" title="删除' + rec[App_DEPARTModel.titleField] + '" />';
					    }
					} 
              ]
        ],
        onDblClickRow: function ( rowData) {
            fun_ShowApp_DEPARTEditForm(rowData);
        },
        onLoadError: function (res) {
        	App_DEPARTModel.ShowException(res);
        },
        onBeforeLoad: function (row, param) {
            param['catchKey'] = Math.random();
            //param['departId'] = $.cookie('departCode');
            param['name'] = $('#App_DEPART_ss').searchbox('getName');
            param['value'] = $('#App_DEPART_ss').searchbox('getValue');
        },
        onContextMenu: function (e, node) {
            e.preventDefault();
            // select the node
            $('#App_DEPART_Grid').treegrid('select', node[App_DEPARTModel.idField]);
            // display context menu
            $('#treemm').menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    });
});


 
